package org.biubiu0002.baiblog.core.controller;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.biubiu0002.baiblog.common.exception.BaiBlogException;
import org.biubiu0002.baiblog.common.exception.BizCodeEnumException;
import org.biubiu0002.baiblog.common.utils.R;
import org.biubiu0002.baiblog.core.annotations.PermissionIgnore;
import org.biubiu0002.baiblog.core.model.dto.TokenUserDto;
import org.biubiu0002.baiblog.core.model.vo.req.ReadMessageReqVo;
import org.biubiu0002.baiblog.core.service.ConversationService;
import org.biubiu0002.baiblog.core.utils.SecurityUtils;
import org.biubiu0002.baiblog.im.dto.conversation.ConversationCardDto;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: biubiu0002
 * @Date: 2022/11/22 16:00
 **/
@RestController
@RequestMapping("/conversation")
public class ConversationController {
    @Resource
    private ConversationService conversationService;

    @GetMapping("/list")
    //调试用
    @PermissionIgnore
    public R list(){
        TokenUserDto user = SecurityUtils.getUser();
        if(user==null){
            throw new BaiBlogException(BizCodeEnumException.NOT_LOGIN_EXCEPTION);
        }
        ConversationCardDto conversationCardList = conversationService.getConversationCard(user.getUserUid());
        return R.ok().put("data",conversationCardList);
    }

    @PostMapping("/read")
    //调试用
    @PermissionIgnore
    public R readMessage(@RequestBody ReadMessageReqVo readMessageReqVo){
        String sessionId =  readMessageReqVo.getSessionId();
        Preconditions.checkArgument(StringUtils.isNotBlank(sessionId),"sessionId不能为空");
        TokenUserDto user = SecurityUtils.getUser();
        if(user==null){
            throw new BaiBlogException(BizCodeEnumException.NOT_LOGIN_EXCEPTION);
        }
        conversationService.readUnreadMessage(user.getUserUid(), sessionId);
        return R.ok();
    }


}
